
package org.sopt.dev.ImgKnight.monitor.gui.builder;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.sopt.dev.ImgKnight.monitor.Monitor;
import org.sopt.dev.ImgKnight.monitor.dialog.AboutDialog;
import org.sopt.dev.ImgKnight.monitor.dialog.ConsoleDialog;
import org.sopt.dev.ImgKnight.monitor.resource.ResourceManager;

public class CoolBarBuilder implements Builder {

	private Monitor monitor = null;
	
	public CoolBarBuilder(Monitor monitor) {
		this.monitor = monitor;
	}
	
	@Override
	public void build() {
		CoolBar coolBar = new CoolBar(monitor.getShell(), SWT.FLAT);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		coolBar.setLayoutData(gridData);
		
		ToolBar exitToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem exit = new ToolItem(exitToolBar, SWT.PUSH);
		exit.setImage(ResourceManager.loadImage(monitor.getDisplay(), "exit.png"));
		exit.setToolTipText("Exit");
		exit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				monitor.getShell().close();
			}
		});
		CoolItem coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(exitToolBar);		
		Point size = exitToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		Point coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar terminalToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem terminal = new ToolItem(terminalToolBar, SWT.PUSH);
		terminal.setImage(ResourceManager.loadImage(monitor.getDisplay(), "terminal.png"));
		terminal.setToolTipText("Terminal");
		terminal.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ConsoleDialog consoleDialog = new ConsoleDialog(monitor.getDisplay(), monitor.getShell());
				consoleDialog.open();
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(terminalToolBar);
		size = terminalToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar trayToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem tray = new ToolItem(trayToolBar, SWT.PUSH);
		tray.setImage(ResourceManager.loadImage(monitor.getDisplay(), "tray.png"));
		tray.setToolTipText("Tray");
		tray.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				monitor.getShell().setVisible(false);
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(trayToolBar);
		size = trayToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar gcToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem gc = new ToolItem(gcToolBar, SWT.PUSH);
		gc.setImage(ResourceManager.loadImage(monitor.getDisplay(), "gc.png"));
		gc.setToolTipText("Garbage Collector");
		gc.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				monitor.writeGcSignal(monitor.getContentsBuilder().getCurrentSelectedPeerIpAddress());
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(gcToolBar);
		size = gcToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar viewToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem freqUp = new ToolItem(viewToolBar, SWT.PUSH);
		ToolItem freqDown = new ToolItem(viewToolBar, SWT.PUSH);
		freqUp.setImage(ResourceManager.loadImage(monitor.getDisplay(), "up.png"));
		freqUp.setToolTipText("Frequency Up");
		freqUp.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int currFreq = monitor.getFrequency();
				if( currFreq > 0 ) {
					monitor.setFrequency(--currFreq);
					monitor.getMenuBuilder().setSelectedFreq(currFreq);
				}
			}
		});
		freqDown.setImage(ResourceManager.loadImage(monitor.getDisplay(), "down.png"));
		freqDown.setToolTipText("Frequency Down");
		freqDown.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				int currFreq = monitor.getFrequency();
				if( currFreq < 3 ) {
					monitor.setFrequency(++currFreq);
					monitor.getMenuBuilder().setSelectedFreq(currFreq);
				}
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(viewToolBar);
		size = viewToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = viewToolBar.computeSize(size.x, size.y);
		coolItem.setMinimumSize(78, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar infoToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem info = new ToolItem(infoToolBar, SWT.PUSH);
		info.setImage(ResourceManager.loadImage(monitor.getDisplay(), "info.png"));
		info.setToolTipText("About");
		info.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				AboutDialog dialog = new AboutDialog(monitor.getDisplay(), monitor.getShell());
				dialog.open();
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(infoToolBar);
		size = infoToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
		
		ToolBar helpToolBar = new ToolBar(coolBar, SWT.FLAT);
		ToolItem help = new ToolItem(helpToolBar, SWT.PUSH);
		help.setImage(ResourceManager.loadImage(monitor.getDisplay(), "help.png"));
		help.setToolTipText("Help");
		help.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				/* TODO */
			}
		});
		coolItem = new CoolItem(coolBar, SWT.NONE);
		coolItem.setControl(helpToolBar);
		size = helpToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		coolSize = coolItem.computeSize(size.x, size.y);
		coolItem.setMinimumSize(39, coolSize.y);
		coolItem.setPreferredSize(coolSize);
		coolItem.setSize(coolSize);
	}
	
}
